define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var muiTips = require("muiTips"); //封装的弹框

	var dingTalkNav = require('dd_config');
	var keyWordText = ""; //关键字搜索
	var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
	function getRightFun() {
		document.location.href = "../../src/chioceProduct/newProduct.html?dd_nav_bgcolor=FFFF943E";
	}
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authorityGoods = authorityInit.getJurisdiction(authorityData, sourceType.INFO_GOODS); //货品权限判断
	if(authorityIdList.indexOf(sourceType.INFO_GOODS) === -1) {
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限查看货品资料哦!");
		$("#addGoods").hide();
		$("ul.pop-tab > li").addClass('mui-disabled');
	} else {
		if(authorityGoods.indexOf(2) === -1) {
			$("header > a").remove();
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("#addGoods").show();
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authorityGoods.indexOf(1) === -1) {
			$('.mui-content > .tipsConInit').html("您没有权限查看货品资料哦!");
			$('.mui-content').addClass("initContentTips");
			$("ul.pop-tab > li").addClass('mui-disabled');
		} else {
			var getSearchItem = require("getSearchItem"); //调用封装搜索
			getSearchItem.getSearchItem("#chioce-search"); //添加搜索效果
			getSearchItem.noSearchTap("ul.pop-tab .pop-tab-titleitem"); //点击其他遮罩层关闭搜索
			$("body").on("keyup", "#searchText", function() { //搜索框事件
				keyWordText = $("#searchText").val();
				dataState = true;
				getGoodsList("productlist", "#mui-list", "searchContentTips", catalogId, orderBy, false);
			});

			//扫码获取货品详情
			mui("body").on("tap", "#scanImg", function() {
				dingTalkNav.ScanCode(ScanCode);
			});

			var getFunList = require("../getFunList/getFunList.js"); //函数配置js
			//调用日期筛选传值
			require("getSearchDateItem");
			//调用状态筛选值
			require("recordStatusID");
			var catalogId = 0;
			var orderBy = "";
			var startDate = ''; //开始时间
			var endDate = ''; //结束时间
			var strStatusId = []; //id筛选
			strStatusId[0] = '';
			var pageIndex = 1; //当前分页

			var pageState = 10; //判断有多少页，调用接口再动态改
			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			//  mui('.mui-scroll-wrapper').scroll({
			//		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
			//	});

			mui("body").on("tap", "#sortListShow li", function() {
				catalogId = $(this).find("a").attr("data-id");
				dataState = true;
				getGoodsList("productlist", "#mui-list", "searchContentTips", catalogId, orderBy, false);
			});
			mui("body").on("tap", "#orderByDate li", function() {
				orderBy = "DataModified " + $(this).attr("data-orderby");
				dataState = true;
				getGoodsList("productlist", "#mui-list", "searchContentTips", catalogId, orderBy, false);
			});

			//	    //按时间筛选数据
			//		mui('body').on('tap', '.pop-tab-con1 li', function() {
			//			pageIndex=1;
			//			startDate=$(this).attr('data-startDate');
			//			endDate=$(this).attr('data-endDate');
			//			dataState=true;
			//			getGoodsList("productlist","#mui-list","searchContentTips");
			////			mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);         //返回顶部
			//		});
			//		//按状态筛选数据
			//		mui('body').on('tap', '.pop-tab-con2 li', function() {
			//			pageIndex=1;
			//			strStatusId[0]=$(this).attr('data-StatusId');
			//			dataState=true;
			//	//		mui('#pullrefresh').pullRefresh().pullupLoading();
			//			getGoodsList("productlist","#mui-list","searchContentTips");
			////			mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);         //返回顶部
			//		});

			var dataCached = [];

			//初始化页面数据
			getGoodsList("productlist", "#mui-list", "initContentTips", catalogId, orderBy, true);
		}
	}

	function getGoodsList(templateId, mainId, contentTips, catalogId, orderBy, isChioce) {
		//alert(catalogId);
		var option = {};
		var dataModel = {
			"keyword": keyWordText, //(string, optional): 关键字 ,
			"catalogId": catalogId, //(integer, optional): 货品分类Id ,
			"orderBy": orderBy, //(string, optional): 排序字段 ,
			"whId": 0, //(integer, optional): 仓库id ,
			"pageIndex": pageIndex, //(integer, optional),
			"isBom": false,
			"pageSize": 15 //(integer, optional): 分页数；最大分页支持100
		};
		abp.services.app.infoGoods.getPage(dataModel, option)
			.done(function(data) {
				console.log(data);
				getFunList.dataCached = [];
				for(var i = 0; i < data.items.length; i++) {
					getFunList.dataCached[data.items[i].id] = data.items[i];
				}
				var totalCountNum = data.totalCount; //获取数据列表条数
				pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
				if(totalCountNum < 1) { //动态改变无数据提示
					if(isChioce === true) {
						$('.mui-content > .tipsConInit').html("还没有货品资料哦!");
					}
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
					$('.mui-content').addClass(contentTips);
					$('.mui-pull-top-pocket').css('display', 'none');
				} else {
					$('.mui-content').removeClass(contentTips);
				}
				//		        	var items = data.items;
				////		        	$.each(items, function(i,v) {
				////		        		var serialDate = v.serialDate.substring(0,10);
				////		        		var dataCreated = v.dataCreated.substring(0,10);
				////		        		v.dataCreated = dataCreated;
				////		        		v.serialDate = serialDate;
				////		        	});
				var html = template(templateId, data);
				if(dataState === true) { //判断是页面刷新还是上滑追加数据
					$(mainId).html(html);
				} else {
					$(mainId).append(html);
				}

				var statusIdColor = require("statusIdColor");
				statusIdColor($(".statusId"));
				if(data.success) {

				} else {
					//	            		console.log(data.error.message)

				}
			}).fail(function(data) {

			});
	}
	//		下滑刷新具体业务实现
	function pulldownRefresh() {
		startDate = '';
		endDate = '';
		strStatusId[0] = '';
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			getGoodsList("productlist", "#mui-list", "searchContentTips", catalogId, orderBy, false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);
	}
	//		上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			getGoodsList("productlist", "#mui-list", "searchContentTips", catalogId, orderBy, false);
		}, 1500);
	}

	//扫码获取货品单号跳转详情页
	function ScanCode(data) {
		var option = {};
		var dataModel = {
			"barCode": data.text //"string"
		};
		abp.services.app.infoGoods.getModelByBarCode(dataModel, option)
			.done(function(data) {
				sessionStorage.setItem('data-stockQuery', JSON.stringify(data));
				sessionStorage.flags = "show";
				if(sessionStorage.formIndex) {
					document.location.href = "../../src/stock/stockDetail.html?dd_nav_bgcolor=FFFF943E";
				} else {
					document.location.href = "../../src/chioceProduct/productDetail.html?dd_nav_bgcolor=FFFF943E";
				}
			})
			.fail(function(err) {
				$("#mui-list").html("");
				$('.mui-content').addClass("searchContentTips");
			});

	}
});